package com.example.myprojectoffeixue;
import android.app.Dialog;
import android.content.Context;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.alibaba.fastjson.JSON;
import com.localization.Gateway;
import com.localization.GatewayRequestUtil;
import com.localization.SendUtil;
import org.java_websocket.client.WebSocketClient;
import java.util.ArrayList;
//import de.tavendo.autobahn.WebSocketConnection;
public class Listviewadapter extends BaseAdapter {
    private ArrayList<SheBeiClass> d;
    private LayoutInflater layoutInflater;
    private Context context;
    WebSocketClient wsc;
    String cmd;
    private MyKgActivity mg;
    private String ieee;
    private String port;
    private String mac;
    private String s;
    String j;
    public AutoButton autoButton;
    private TextView bt;
    private TextView bt1;
    private TextView bt2;
    private Dialog dialog1;
    private final MyApplication m;
    public LinearLayout le;

    public Listviewadapter(ArrayList<SheBeiClass> d, Context context, String cmd, Dialog dialog1) {
        super();
        this.d = d;
        this.dialog1 = dialog1;
        this.context = context;
        this.cmd = cmd;
        mg = (MyKgActivity) context;
        m = (MyApplication) mg.getApplication();
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return d.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return d.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder h = null;
        if (convertView == null) {
            h = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.item_list, null);
            h.tv = (TextView) convertView.findViewById(R.id.tv_listview);
            h.iv = (ImageView) convertView.findViewById(R.id.iv_listview);
            h.iv1 = (ImageView) convertView.findViewById(R.id.iv1_listview);
            h.l = (LinearLayout) convertView.findViewById(R.id.bt_lin);
            //h.l1 = (LinearLayout) convertView.findViewById(R.id.l1_listview);
            if ("1008".equals(cmd)) {
                h.l.setVisibility(View.VISIBLE);
                h.iv1.setVisibility(View.GONE);
                //h.l1.setVisibility(View.GONE);
            } else if ("YKQ".equals(cmd)) {
                //h.l1.setVisibility(View.GONE);
                h.iv1.setVisibility(View.GONE);
            } else if (cmd == null) {
                h.iv1.setClickable(false);
            }
            convertView.setTag(h);
        } else {
            h = (ViewHolder) convertView.getTag();
        }
        h.tv.setText(d.get(position).deviceName);

        if (d.get(position).deviceId != null) {
            if ("0100".equals(d.get(position).deviceId)) {
                //墙面三路开关
                if ("01".equals(d.get(position).status)) {
                    h.iv.setImageResource(R.drawable.kai);
                    h.iv1.setImageResource(R.drawable.anguan);
                } else if ("00".equals(d.get(position).status)) {
                    h.iv.setImageResource(R.drawable.guan);
                    h.iv1.setImageResource(R.drawable.ankai);
                }
            } else if ("010B".equals(d.get(position).deviceId) || "010A".equals(d.get(position).deviceId)||"0110".equals(d.get(position).deviceId)) {
                //智能门锁
                if ("01".equals(d.get(position).status)) {
                    h.iv.setImageResource(R.drawable.ms);
                    h.iv1.setImageResource(R.drawable.anguan);

                } else if ("00".equals(d.get(position).status)) {
                    h.iv.setImageResource(R.drawable.msguan);
                    h.iv1.setImageResource(R.drawable.ankai);

                }
            } else if ("010D".equals(d.get(position).deviceId)) {
//门磁

                if ("01".equals(d.get(position).status)) {
                    h.iv.setImageResource(R.drawable.mc);
                    h.iv1.setImageResource(R.drawable.anguan);
                } else if ("00".equals(d.get(position).status)) {
                    h.iv.setImageResource(R.drawable.mcguan);
                    h.iv1.setImageResource(R.drawable.ankai);
                }

            } else if ("010E".equals(d.get(position).deviceId)) {
//红外
                if ("01".equals(d.get(position).status)) {
                    h.iv.setImageResource(R.drawable.hw2);
                    h.iv1.setImageResource(R.drawable.anguan);
                } else if ("00".equals(d.get(position).status)) {
                    h.iv.setImageResource(R.drawable.hw);
                    h.iv1.setImageResource(R.drawable.ankai);

                } else if ("04".equals(d.get(position).status)) {
                    h.iv.setImageResource(R.drawable.hw2);
                    h.iv1.setImageResource(R.drawable.anguan);
                }

            } else if ("010C".equals(d.get(position).deviceId)) {
//取电
                if ("01".equals(d.get(position).status)) {
                    h.iv.setImageResource(R.drawable.qudiana);
                    h.iv1.setImageResource(R.drawable.anguan);

                } else if ("00".equals(d.get(position).status)) {
                    h.iv.setImageResource(R.drawable.qudianguan);
                    h.iv1.setImageResource(R.drawable.ankai);

                }

            } else if ("0002".equals(d.get(position).deviceId)) {
                h.iv.setImageResource(R.drawable.cl2);
                le = h.l;
                if ("00".equals(d.get(position).status)) {
                    le.setBackgroundResource(R.drawable.clg);
                } else if ("01".equals(d.get(position).status)) {
                    le.setBackgroundResource(R.drawable.clk);

                } else if ("02".equals(d.get(position).status)) {
                    le.setBackgroundResource(R.drawable.clt);

                } else {

                    le.setBackgroundResource(R.drawable.clt);
                }

            } else if ("0009".equals(d.get(position).deviceId)) {
                if ("01".equals(d.get(position).status)) {
                    h.iv.setImageResource(R.drawable.chazuo);
                    h.iv1.setImageResource(R.drawable.anguan);

                } else if ("00".equals(d.get(position).status)) {
                    h.iv.setImageResource(R.drawable.chazuokai);
                    h.iv1.setImageResource(R.drawable.ankai);
                }


            } else if ("0020".equals(d.get(position).deviceId)) {
                h.iv.setImageResource(R.drawable.hwzfq);
            }
        }
        h.iv1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("点击了","点击");
                if (!"".equals(cmd)) {

                    if (Gateway.MacSocke.containsKey(d.get(position).mac)) {
                        Log.d("走内网","内网--"+d.get(position).deviceId);
                        if ("010B".equals(d.get(position).deviceId) || "010A".equals(d.get(position).deviceId)||"0110".equals(d.get(position).deviceId)) {
                            //智能门锁
                            if (!"01".equals(d.get(position).status)) {
                                Log.d("开锁","开锁");
                                String cmd = GatewayRequestUtil.createLockOpenStr(d.get(position).ieee, d.get(position).port);
                                SendUtil.Mac = d.get(position).mac;
                                SendUtil.cmd = cmd;
                                SendUtil.hand.sendEmptyMessage(0);
                            }
                        }
                    else if ("010E".equals(d.get(position).deviceId)) {
//红外
                        if ("00".equals(d.get(position).status)) {
                            String cmd = GatewayRequestUtil.createTurnOnMagnetStr(d.get(position).ieee, d.get(position).port);
                            SendUtil.Mac = d.get(position).mac;
                            SendUtil.cmd = cmd;
                            SendUtil.hand.sendEmptyMessage(0);
                        } else if ("01".equals(d.get(position).status)) {
                            String cmd = GatewayRequestUtil.createTurnOffMagnetStr(d.get(position).ieee, d.get(position).port);
                            SendUtil.Mac = d.get(position).mac;
                            SendUtil.cmd = cmd;
                            SendUtil.hand.sendEmptyMessage(0);
                        }

                    } else if ("010C".equals(d.get(position).deviceId)) {
//取电
                        if ("01".equals(d.get(position).status)) {
                            Log.d("开启取电","开启");
                            String cmd = GatewayRequestUtil.createPowerOnOffStr(d.get(position).ieee, d.get(position).port, "00");
                            SendUtil.Mac = d.get(position).mac;
                            SendUtil.cmd = cmd;
                            SendUtil.hand.sendEmptyMessage(0);

                        } else if ("00".equals(d.get(position).status)) {
                            Log.d("关闭取电","关闭");
                            String cmd = GatewayRequestUtil.createPowerOnOffStr(d.get(position).ieee, d.get(position).port, "01");
                            SendUtil.Mac = d.get(position).mac;
                            SendUtil.cmd = cmd;
                            SendUtil.hand.sendEmptyMessage(0);


                        }

                    }
                        /*else if ("0002".equals(d.get(position).deviceId)) {

                        if ("00".equals(d.get(position).status)) {
                            String cmd = GatewayRequestUtil.createCurtainStr(d.get(position).ieee, d.get(position).port, "00");
                            SendUtil.Mac = d.get(position).mac;
                            SendUtil.cmd = cmd;
                            SendUtil.hand.sendEmptyMessage(0);
                            //窗帘关
                        } else if ("01".equals(d.get(position).status)) {


                            //窗帘开

                        } else if ("02".equals(d.get(position).status)) {
                            String cmd = GatewayRequestUtil.createCurtainStr(d.get(position).ieee, d.get(position).port, "02");//窗帘停
                            SendUtil.Mac = d.get(position).mac;
                            SendUtil.cmd = cmd;
                            SendUtil.hand.sendEmptyMessage(0);
                        }
                    } */else if ("0009".equals(d.get(position).deviceId)) {
                        if ("01".equals(d.get(position).status)) {

                            String cmd = GatewayRequestUtil.createSwitchOnOffStr(d.get(position).mac, d.get(position).ieee, d.get(position).port, "00");
                            SendUtil.Mac = d.get(position).mac;
                            SendUtil.cmd = cmd;
                            SendUtil.hand.sendEmptyMessage(0);

                            //插座关
                        } else if ("00".equals(d.get(position).status)) {

                            String cmd = GatewayRequestUtil.createSwitchOnOffStr(d.get(position).mac, d.get(position).ieee, d.get(position).port, "01");

                            SendUtil.Mac = d.get(position).mac;
                            SendUtil.cmd = cmd;
                            SendUtil.hand.sendEmptyMessage(0);


                            //插座开


                        }


                    }

                } else {
                    wsc = m.c;
                    if (d.size() != 0) {
                        s = d.get(position).status;
                        ieee = d.get(position).ieee;
                        port = d.get(position).port;
                        mac = d.get(position).mac;

                        if ("00".equals(s)) {
                            s = "01";
                        } else {
                            s = "00";
                        }


                        Kuaiguanutil k = new Kuaiguanutil();
                        k.cmd = cmd;
                        k.ieee = ieee;
                        k.port = port;
                        k.status = s;
                        k.mac = mac;
                        String j = JSON.toJSONString(k);
                        //wsc.sendTextMessage(j);
                        if (m.flag3) {
                            wsc.send(j);
                            dialog1.show();
                            new Thread() {

                                @Override
                                public void run() {
                                    for (int i = 0; i < 10; i++) {
                                        if (mg.flag) {
                                            try {
                                                if (!dialog1.isShowing()) {
                                                    return;
                                                }
                                                sleep(500);
                                            } catch (InterruptedException e) {
                                                e.printStackTrace();
                                            }
                                        } else {
                                            return;
                                        }
                                    }
                                    m.hand.sendEmptyMessage(55);
                                    super.run();
                                }
                            }.start();
                        } else {
                            ToastUtils.showToast(context, "与后台服务器连接断开，正在重连");
                        }

                    }
                }

            }}
        });


        bt = (TextView) h.l.findViewById(R.id.bt_one);
        bt1 = (TextView) h.l.findViewById(R.id.bt_two);
        bt2 = (TextView) h.l.findViewById(R.id.bt_three);

        bt.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                wsc = m.c;
                if (d.size() != 0) {

                    if(Gateway.MacSocke.containsKey(d.get(position).mac)){
                        String cmd = GatewayRequestUtil.createCurtainStr(d.get(position).ieee, d.get(position).port, "04");
                        SendUtil.Mac = d.get(position).mac;
                        SendUtil.cmd = cmd;
                        SendUtil.hand.sendEmptyMessage(0);
                        d.get(position).status = "01";
                        MyApplication.hand.sendEmptyMessage(0);

                    }else{
                    s = "01";
                    Kuaiguanutil k = new Kuaiguanutil();
                    k.ieee = d.get(position).ieee;
                    k.port = d.get(position).port;
                    k.status = s;
                    k.cmd = "1008";
                    k.mac = d.get(position).mac;
                    j = JSON.toJSONString(k);
                    Log.d("cmd", j);
                    if (m.flag3) {
                        wsc.send(j);
                    }
                    d.get(position).status = "01";
                    if (m.hand != null) {
                        m.hand.sendEmptyMessage(0);
                    }
                }
                }




            }
        });

        bt1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                wsc = m.c;
                if (d.size() != 0) {
                    if(Gateway.MacSocke.containsKey(d.get(position).mac)){
                        String cmd = GatewayRequestUtil.createCurtainStr(d.get(position).ieee, d.get(position).port, "06");
                        SendUtil.Mac = d.get(position).mac;
                        SendUtil.cmd = cmd;
                        SendUtil.hand.sendEmptyMessage(0);
                        d.get(position).status = "02";
                        MyApplication.hand.sendEmptyMessage(0);

                    }else{
                    s = "02";
                    Kuaiguanutil k = new Kuaiguanutil();
                    k.ieee = d.get(position).ieee;
                    k.port = d.get(position).port;
                    k.cmd = "1008";
                    k.status = s;
                    k.mac = d.get(position).mac;
                    j = JSON.toJSONString(k);
                    Log.d("cmd", j);
                    if (m.flag3) {
                        wsc.send(j);
                    }

                    d.get(position).status = "02";
                    if (m.hand != null) {
                        m.hand.sendEmptyMessage(0);
                    }

                }
                }
            }
        });

        bt2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                wsc = m.c;
                if (d.size() != 0) {
                    if(Gateway.MacSocke.containsKey(d.get(position).mac)){
                        String cmd = GatewayRequestUtil.createCurtainStr(d.get(position).ieee, d.get(position).port, "05");
                        SendUtil.Mac = d.get(position).mac;
                        SendUtil.cmd = cmd;
                        SendUtil.hand.sendEmptyMessage(0);
                        d.get(position).status = "00";
                        MyApplication.hand.sendEmptyMessage(0);

                    }else{
                    s = "00";
                    Kuaiguanutil k = new Kuaiguanutil();
                    k.ieee = d.get(position).ieee;
                    k.port = d.get(position).port;
                    k.cmd = "1008";
                    k.status = s;
                    k.mac = d.get(position).mac;
                    j = JSON.toJSONString(k);
                    Log.d("cmd", j);
                    if (m.flag3) {
                        wsc.send(j);
                    }
                    d.get(position).status = "00";
                    if (m.hand != null) {
                        m.hand.sendEmptyMessage(0);
                    }

                }}
            }
        });
        return convertView;
    }

    class ViewHolder {

        TextView tv;
        ImageView iv;
        LinearLayout l1;
        ImageView iv1;
        LinearLayout l;

    }

}